#!/bin/bash
: <<COMMENT
  Copyright (C) 2012 Tri Le <trile7 at gmail dot com>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation version 3.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
COMMENT

blue='\e[0;34m'; green='\e[0;32m'; red='\e[0;31m'; yellow='\e[0;33m'; bold='\e[1m';none='\e[0m'
function menu {
  n=0
  choices=("$@")
  echo -e "$blue$bold$menutitle"
  for i in "${choices[@]}"; do
    echo -e "$none$((n++)))   $yellow$i"
  done
  echo -en "${none}c|q) ${yellow}Cancel"
  echo -e $green
  read -p "Enter a choice from above menu: " i
  echo -e $none
  [[ $i =~ c|q ]] && exit 1
  if test $i -lt $n 2>/dev/null; then
    choice=${choices[i]}; return $i
  else
    echo -e "$red$i is an invalid entry...please try again!"
    menu "${choices[@]}"
  fi
  }

function slideshow {
  echo "Default delay 2 seconds, loop forever, and resize only large image to 800x600:"
  echo "  -delay 2 -loop 0 -resize 800x600>"
  echo -ne $green
  read -p "Enter slideshow options (enter to use defaults): " -a opt
  echo -ne $none
  for (( i=0; i<=${#opt[@]}; i++ )); do
    [[ ${opt[i]} = *delay ]] && d=${opt[i+1]}
    [[ ${opt[i]} = *loop ]] && l=${opt[i+1]}
    [[ ${opt[i]} = *resize ]] && s=${opt[i+1]}
  done
  display -coalesce -auto-orient -immutable -delay ${d:=2} -loop ${l:=0} -resize ${s:=800x600>} "${files[@]}"
  }

function info {
  echo -e "${yellow}image, format, dimension, geometry, depth, class, size, processing times $none"
  for i in "${files[@]}"; do identify "$i"; done
  }

function wallpaper {
  rootsize=`xdpyinfo | grep dimension | awk '{print $2}'`
  rootw=`echo $rootsize | cut -d'x' -f1`
  rooth=`echo $rootsize | cut -d'x' -f2`
  img=/tmp/.wallpaper.jpg
  menutitle="---Wallpaper Modes---"
  menu "Tile" "Resize and Tile" "Center" "Resize and Center" "Stretch"
  case $choice in
    Tile) display -window root "$1"; exit ;; #tile
    *Resize*) convert "$1" -resize $rootsize "$img" ;; #resize
    Stretch) convert "$1" -resize $rootsize! "$img" ;; #stretch
    *) convert "$1" -resize $rootsize\> "$img" ;;
  esac
  if [[ $choice = *Center* ]]; then
    imgsize=`identify -format "%wx%h" $img`
    imgw=`echo $imgsize | cut -d'x' -f1`
    imgh=`echo $imgsize | cut -d'x' -f2`
    x=$((rootw-imgw)); x=$((x/2))
    y=$((rooth-imgh)); y=$((y/2))
    display -geometry $rootsize+$x+$y -window root $img
  else
    display -window root $img
  fi
  }

function resize {
  echo -e "Use resize operators ${green}> < !${none} to determine what image size to resize.  Examples:"
  echo -e "  ${green}800x600>${none} reduce only larger image to given size keeping aspect ratio"
  echo -e "  ${green}800x600<${none} enlarge only smaller image to given size keeping aspect ratio"
  echo -e "  ${green}800x600!${none} force image to given size ignoring aspect ratio"
  echo -e "  ${green}800x600${none}  resize to given size keeping aspect ratio"
  echo -ne $green
  read -p "Enter new size (WxH in pixel): " s
  echo -e $none
  if mkdir -p $s 2>/dev/null; then
    for i in "${files[@]}"; do convert "$i" -resize "$s" "$s/${i##*/}"; done
  else
    echo -e "${red}A value for size is required $none"
    resize "$@"
  fi
  }

function rotate {
  echo -ne $green
  read -p "Enter rotation degree (non-zero number): " d
  echo -ne $none
  if [[ $d -gt 0 ]] || [[ $d -lt 0 ]]; then
    for i in "${files[@]}"; do
      [[ $i = *.* ]] && ext=.${i##*.}
      convert "$i" -rotate $d ${i%.*}_rot$d$ext
    done
  else
    echo -e "${red}Rotation degree must be a non-zero number $none"
    rotate "$@"
  fi
  }

function format {
  menutitle="---Image Formats---"
  menu "jpg" "png" "gif" "ico" "bmp" "tif" "ps" "pdf"
  mkdir -p $choice
  for i in "${files[@]}"; do
    case $choice in
      tif|ps|pdf) convert "$i" -compress Zip "$choice/${i%.*}.$choice" ;;
      bmp) convert "$i" BMP3:"$choice/${i%.*}.$choice" ;;
      *) convert "$i" "$choice/${i%.*}.$choice" ;;
    esac
  done
  }

function 2pdf {
  echo -ne $green
  read -p "Enter pdf filename: " f
  echo -ne $none
  if [[ -z $f ]]; then
    f=combined.pdf
  elif [[ $f != *.pdf ]]; then
    f=$f.pdf
  fi
  [[ -e $f ]] && f=${f%.*}.`date +%s`.pdf
  mkdir -p `dirname "$f"`
  convert "${files[@]}" -compress Zip "$f"
  }

function usage {
  echo -e "${bold}Usage: ${0##*/} [option] image1 image2 ..... $none"
  echo "Options: menu is shown if option is not specified"
  echo "  --slideshow  display images as slideshow"
  echo "  --info       display images information"
  echo "  --wallpaper  set image as wallpaper"
  echo "  --resize     resize images"
  echo "  --rotation   rotate images"
  echo "  --format     change image format"
  echo "  --2pdf       combine images to one pdf file"
  exit
  }

function mainmenu {
  if [[ -z $1 ]]; then
    menutitle="---Image Tools---"
    menu "Image Slideshow" "Image Information" "Set Image as Wallpaper" "Image Resize" "Image Rotation" "Image Format Conversion" "Combine Images to PDF"
    case $choice in
      *Slideshow) slideshow ;;
      *Info*) info "$@" ;;
      *Wallpaper) wallpaper "$1" ;;
      *Resize) resize ;;
      *Rotation) rotate ;;
      *Format*) format ;;
      *PDF) 2pdf ;;
    esac
  else
    $cmd
  fi
  mainmenu
  }

case $1 in
  -h|--h*) usage ;;
  --*) cmd=${1#--}; shift; files=("$@") ;;
  *) files=("$@") ;;
esac
[[ $# -eq 0 ]] && usage
mainmenu $cmd
